import pandas as pd
class bi:#满足工作笔数就触发;确认了涨跌确认标识
    def __init__(self,bikey,work_num=2,check_data=[],save_keys=[],iscut=True,highkey="high",lowkey="low"):
        self.up_bis=[]#存放无限
        self.down_bis=[]#存放无限
        self.bikey=bikey#区别比的关键字，-2,2的转折点信号
        self.his_data=[]#存放进行中笔数的
        self.fx=None
        self.current_bi=None
        self.work_num=work_num#如果iscut=True 工作
        self.work_num1=round(self.work_num / 2)
        self.check_data=check_data#检查数据点None不工作
        self.save_keys=save_keys#保存额外的信息
        self.iscut=iscut#是否缩减到工作数量
        self.highkey=highkey
        self.lowkey=lowkey
        self.sure_up=[]
        self.sure_down=[]
    def get_curr_r(self):
        b = self.his_data[-1]
        if self.current_bi["flag"]=="max":#down
            hisd = pd.DataFrame(self.his_data)
            hisd.index = hisd["timekey"]
            minprice = hisd[self.lowkey].min()
            min_timekey = hisd.loc[hisd[self.lowkey] == minprice].index.tolist()[0]
            r=(minprice-self.current_bi["price"])/self.current_bi["price"]
            bi=(self.current_bi,{"timekey":min_timekey,"price":minprice,"flag":"min","confirm_timkey":b["timekey"],"confirm_price":b["close"]})
            if self.save_keys:
                for i in self.save_keys:
                    bi[i]=b[i]
            return r,bi
        else:#up
            hisd = pd.DataFrame(self.his_data)
            hisd.index = hisd["timekey"]
            maxprice = hisd[self.highkey].max()
            max_timekey = hisd.loc[hisd[self.highkey] == maxprice].index.tolist()[0]
            r = (maxprice - self.current_bi["price"]) / self.current_bi["price"]
            bi = (self.current_bi, {"timekey": max_timekey, "price": maxprice, "flag": "max","confirm_timkey":b["timekey"],"confirm_price":b["close"]})
            if self.save_keys:
                for i in self.save_keys:
                    bi[i]=b[i]
            return r, bi

    def onbar(self,b):
        rst=None
        if b[self.bikey]==-2:#死叉
            self.sure_down.append(b["timekey"])
            if self.his_data:
                hisd=pd.DataFrame(self.his_data)
                hisd.index=hisd["timekey"]
                maxprice=hisd[self.highkey].max()
                max_timekey=hisd.loc[hisd[self.highkey]==maxprice].index.tolist()[0]
                bb=self.his_data.pop(0)
                while bb["timekey"]<max_timekey:
                    bb = self.his_data.pop(0)
                bi_max={"timekey":max_timekey,"price":maxprice,"flag":"max","confirm_timkey":b["timekey"],"confirm_price":b["close"]}
                if self.save_keys:
                    for i in self.save_keys:
                        bi_max[i]=b[i]
                if self.current_bi:
                    self.up_bis.append((self.current_bi,bi_max))
                    if self.iscut:
                        while len(self.up_bis)>self.work_num:
                            self.up_bis.pop(0)
                self.current_bi=bi_max
                rst="sell"
            self.fx="down"
        elif b[self.bikey]==2:#金叉
            self.sure_up.append(b["timekey"])
            if self.his_data:
                hisd = pd.DataFrame(self.his_data)
                hisd.index = hisd["timekey"]
                minprice = hisd[self.lowkey].min()
                min_timekey = hisd.loc[hisd[self.lowkey] == minprice].index.tolist()[0]
                bb = self.his_data.pop(0)
                while bb["timekey"] < min_timekey:
                    bb = self.his_data.pop(0)
                bi_min={"timekey": min_timekey, "price": minprice,"flag":"min","confirm_timkey":b["timekey"],"confirm_price":b["close"]}
                if self.save_keys:
                    for i in self.save_keys:
                        bi_min[i]=b[i]
                if self.current_bi:
                    self.down_bis.append((self.current_bi, bi_min))
                    if self.iscut:
                        while len(self.down_bis)>self.work_num:
                            self.down_bis.pop(0)
                self.current_bi=bi_min
                rst = "buy"
            self.fx = "up"
        self.his_data.append(b)
        if rst=="buy":
            if len(self.down_bis)<self.work_num1:
                rst=None
        elif rst=="sell":
            if len(self.up_bis) < self.work_num1:
                rst = None
        for i in self.check_data:
            if b[i]  is None:
                rst=None
        return rst
